/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.listeners;

import ftpcombiner.ApplicationController;
import ftpcombiner.filesystem.FileSystem;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author Ben
 */
public class UploadDropTargetListener extends DropTarget {

    @Override
    public int getDefaultActions() {
        return DnDConstants.ACTION_COPY;
    }

    @Override
    public synchronized void dragEnter(DropTargetDragEvent dtde) {
        if (!dtde.getTransferable().isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            dtde.rejectDrag();
        }
    }

    @Override
    public synchronized void drop(DropTargetDropEvent dtde) {
        List<File> javaFiles = null;
        try {
            dtde.acceptDrop(DnDConstants.ACTION_COPY);
            javaFiles = ((List<File>) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor));

        } catch (UnsupportedFlavorException ex) {
            dtde.rejectDrop();
        } catch (IOException ex) {
            ApplicationController.getInstance().setAppStatus("Drop Failed");
            dtde.rejectDrop();
        }
        for (File f : javaFiles) {
           FileSystem.getInstance().upload(f);
        }
    }
}
